package com.inspur.cmis.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.cmis.pojo.User;
import com.inspur.cmis.service.ClientManagerService;
import com.inspur.cmis.service.UserService;

import javafx.print.Printer;
//  nhbhbh
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private ClientManagerService clientMgrService;
	//默认查询
	@RequestMapping("/userInfoList")
	public String userInfoList(Model model, HttpServletRequest request, @RequestParam(value = "pn", defaultValue = "1") Integer pn){
		
		String userId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		Map<String, String> map =new HashMap<String, String>();
		 map.put("userId", userId);
		 map.put("userName",userName);
		 
		PageHelper.startPage(pn,3);
		List<User> userList = userService.getUserList(map);
		PageInfo<User> pageInfo =new PageInfo<User>(userList,userList.size());
		model.addAttribute("userId",userId);
		model.addAttribute("userName",userName);
		model.addAttribute("userList", userList);
		model.addAttribute("pageInfo",pageInfo);
		return "/manager/userInfo.jsp";
	}
	//添加用户
	
	@RequestMapping("/addUser")
	public String addUser( Model model, HttpServletRequest request,User user ) {
		
//		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Date date=new Date(System.currentTimeMillis());
//		sdf.format(date);
		String password= "123456";
		String isEnable ="T";
		user.setPassword(password);
		user.setIsEnable(isEnable);
		System.out.println(user.getBirthDay()+"----------------------");
		int i=userService.addUser2(user);
		model.addAttribute("member",user);
		System.out.println(user.getUserId()+"-----------------------");
		return "userInfoList?userId="+user.getUserId();
		
	}
	
	
	//校验用户名是否已存在
	@ResponseBody
	@RequestMapping("/checkUser")
	public Map checkUser(String username) {
		System.out.println("前台Ajax传递过来的参数: " +username);
		List<User> selectUser = userService.getUserByUserName(username);
		Map map = new HashMap();
		map.put("selectUser", selectUser);
		if (selectUser.size() >= 1) {
			map.put("msg", "已存在该用户");
		}
		System.out.println("map==="+map);
		return map;
	}

	
	
	//修改用户中间过渡
	
	@RequestMapping("/updateUser1")
	public String updateUser1( Model model, HttpServletRequest request ){
		
		String userId = request.getParameter("userId");
		HashMap< String, String> map =new HashMap<String, String>();
		map.put("userId",userId);
		List<User> list=userService.getUserList(map);
		User user=list.get(0);
		
		model.addAttribute("user",user);
		
		
		
		return "manager/userInfoUpdate.jsp";
	}
	
	//修改用户
	
	@RequestMapping("/updateUser")
	public String updateUser( Model model, HttpServletRequest request ){
		String userId=request.getParameter("userId");
		String username=request.getParameter("username");
		String sex =request.getParameter("sex");
		String birthDay =request.getParameter("birthDay");
		String roleId=request.getParameter("roleId");
		HashMap< String, String> map =new HashMap<String, String>();
		map.put("username",username);
		map.put("sex", sex);
		map.put("roleId", roleId);
		map.put("userId",userId+"");
		map.put("birthDay",birthDay);
		int i=userService.updateUser(map);
		if(i==1) {
			model.addAttribute("msg", "修改成功");
		}
		
		return "userInfoList";
	}
	
	
	@RequestMapping("/deleteUser")
	public String deleteUsers(String userIds){
		String usersId[] = userIds.split(",");
		String user_ids[] = new String[usersId.length];
		for(int i=0;i<usersId.length;i++){
			user_ids[i] = usersId[i];
		}
		userService.deleteUser(user_ids);
		clientMgrService.deleteMgr(user_ids);
		//删除数据后要重新查询一次
		return "redirect:userInfoList";
		
	}
	
	//禁用 启用 重置密码
	
	
	@RequestMapping("/updateUsers")
	public String updateUsers(String userIds,String isEnable,String password){
		String usersId[] = userIds.split(",");
		int user_ids[] = new int[usersId.length];
		ArrayList<User> list=new ArrayList<User>();
		if(password != null | password != "") password ="123456";
		for(int i=0;i<usersId.length;i++){
//			user_ids[i] = Integer.parseInt(usersId[i]);
			User u=new User();
			u.setUserId(Integer.parseInt(usersId[i]));
			u.setIsEnable(isEnable);
			u.setPassword(password);
			list.add(u);
		}
		userService.updateUsers(list);
		return "redirect:userInfoList";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
